"""
Support module for logAnalyzer.py to draw the Map and Statistics PDF's.


"""

import numpy as np
import matplotlib as mpl
mpl.use('Agg')
from matplotlib.font_manager import FontProperties
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt



class DrawMap:
	"""
	Draws the map with markers on the cities it's given at start
	
	Keyword arguments:
    title -- Map Title
    cities -- Dictionary containing the cities as key, and coordinates as items
	"""
	def __init__(self,title,cities):
		"""Default Constructor"""
		self.title = title
		self.cities = cities
	
	def createPDF(self):
		"""
		Creates PDF with the map. 
		
		it cycles the dictionary adds the markers on the given cities.
		in the end it saves it to output.pdf on the program location
		
		"""
		m = Basemap(llcrnrlon=-180,llcrnrlat=-80,urcrnrlon=180,urcrnrlat=80,
					projection='cyl',lat_ts=20)
		plt.title(self.title)	
		m.drawcoastlines()
		m.fillcontinents(color='coral',lake_color='aqua')

		m.drawparallels(np.arange(-90.,91.,60.))
		m.drawmeridians(np.arange(-180.,181.,60.))
		m.drawmapboundary(fill_color='aqua')

		i=1
		for x in self.cities.keys():	
			xpt,ypt = m(self.cities[x][1],self.cities[x][2])
			lonpt, latpt = m(xpt,ypt,inverse=True)
			m.plot(xpt,ypt, marker='o', label=str(i)+' - ' + ' ' + str(x) + ' ' + str(self.cities[x][0]) + ' Packets') 
			plt.text(xpt,ypt," "+str(i))
			i +=1

		fig = plt.legend(bbox_to_anchor=(0.25, 0.5),prop={'size':8})
		plt.savefig('output.pdf')	

class statistics:
	"""
	Draws the graphics with the packet statistics given
	
	Keyword arguments:
    stats -- stats from the stats module taken from the logs
    
	"""
	def __init__(self, stats):
		"""Default Constructor"""
		self.stats = stats
	
	def statsPDF(self):
		"""Draws the graphic into trafic.pdf"""
		packetsIn, packetsOut, errors = self.stats.getNumbers()
		totalPackets = packetsIn + packetsOut + errors

		x = np.arange(1,2)
		y = [packetsIn,packetsOut,errors]
		m =np.arange(1,4)

		fig = plt.figure()
		fig.suptitle('Packet Statistics', fontsize = 20)
		labels = ['IN', 'OUT', 'ERRORS']
		width = 1
		bar1 = plt.bar(x,packetsIn,width, color = "b")
		bar2 = plt.bar(x+width,packetsOut,width, color = "y")
		bar3 = plt.bar(x+width+width,errors,width, color = "r")
		plt.ylabel('Packets')
		plt.xticks(m + (width/2.0), labels )
		for m,y in zip(m,y):
			plt.text(m + (width/2.0-0.1), y+1,y)

		fig.autofmt_xdate()
		
		plt.savefig('trafic.pdf')	
